package com.jjlin.chapter_2;

import java.util.*;

public class CD107{

    public static class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val = val;
        }
    }

    public static class DoubleNode{
        public int val;
        public DoubleNode last;
        public DoubleNode next;
        public DoubleNode(int val){
            this.val = val;
        }
    }

    public static Node createNodeList(int[] nums){
        Node head = new Node(nums[0]);
        Node pre = head;
        for(int i = 1; i < nums.length; i++){
            Node newNode  = new Node(nums[i]);
            pre.next = newNode;
            pre = newNode;
        }
        return head;
    }

    public static DoubleNode createDoubleNodeList(int[] nums){
        DoubleNode head = new DoubleNode(nums[0]);
        DoubleNode pre = head;
        for(int i = 1; i < nums.length; i++){
            DoubleNode newNode = new DoubleNode(nums[i]);
            pre.next = newNode;
            newNode.last = pre;
            pre = newNode;
        }
        return head;
    }

    public static Node reverseNodeList(Node head){
        Node cur = head;
        Node pre = null;
        while(cur != null){
            Node tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }

    public static DoubleNode reverseDoubleNodeList(DoubleNode head){
        DoubleNode cur = head;
        DoubleNode pre = null;
        while(cur != null){
            DoubleNode tmp = cur.next;
            cur.next = pre;
            cur.last = tmp;//多了这一步
            pre = cur;
            cur = tmp;
        }
        return pre;
    }

    public static void printNodeList(Node head){
        Node tmp = head;
        while(tmp != null){
            System.out.print(tmp.val + " ");
            tmp = tmp.next;
        }
        System.out.println();
    }

    public static void printDoubleNodeList(DoubleNode head){
        DoubleNode tmp = head;
        while(tmp != null){
            System.out.print(tmp.val + " ");
            tmp = tmp.next;
        }
        System.out.println();
    }

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] nums = new int[n];
        for(int i = 0; i < n; i++)
            nums[i] = sc.nextInt();
        Node List1 = createNodeList(nums);
        List1 = reverseNodeList(List1);

        int m = sc.nextInt();
        nums = new int[m];
        for(int i = 0; i < m; i++)
            nums[i] = sc.nextInt();
        DoubleNode List2 = createDoubleNodeList(nums);
        List2 = reverseDoubleNodeList(List2);
        printNodeList(List1);
        printDoubleNodeList(List2);
    }
}